<?php
/*
 * [JINYUN!] (C)2001-2099 Jinyunweb.com
 * This is NOT a freeware, use is subject to license terms
 *员工管理
*/
defined('BY_JYA') or exit('error');
$_RQ['op']=$_RQ['op']?$_RQ['op']:'list';
if($_RQ['op']=='list'){
	$params=array();
	$params['uniacid']=$_SESSION['uniacid'];
	if(!empty($_RQ['name'])){
		$params['name like']='%'.trim($_RQ['name']).'%';
		$return['params']['name']=$_RQ['name'];
	}
	if(isset($_RQ['phone'])){
		$params['phone']=intval($_RQ['phone']);
		$return['params']['phone']=$_RQ['phone'];
	}
	if(isset($_RQ['status'])){
		$params['status']=intval($_RQ['status']);
		$return['params']['status']=$_RQ['status'];
	}
	$pindex = max(1, intval($_RQ['page']));
	$psize = 10;
	$total='';
	$fields=array('id','uniacid','mid','name','phone','lev','years','iswx','age','headimg','zhengjian','service','remark','islock','isshow','status','address','createtime');
	$list=pdo_getslice('jy_meiye_worker',$params,array($pindex,$psize),$total,$fields,'','id desc');
	if($list){
		foreach($list as &$item){
			$item['lev_name']=pdo_getcolumn('jy_meiye_lev',array('uniacid'=>$_SESSION['uniacid'],'id'=>$item['lev']),'lev_name');
			if(!$item['lev_name']){
				$item['lev_name']='未设置';
			}
		}
	}
	$return['list']=$list;
	$totalpage=ceil($total/$psize);
	$totalpage=intval($totalpage);
	$return['totalpage'] =$totalpage;
	$return['params']['page'] =$pindex;
	$htmls=array(
		'type'=>'list',
		'tabs'=>array(
			array('op'=>'list','title'=>'员工列表'),
			array('op'=>'edit','title'=>'添加员工'),
		),
		'list'=>array('data'=>'list','edit'=>true,'list'=>array(
			array('name'=>'name','title'=>'员工姓名','type'=>'string'),
			array('name'=>'headimg','title'=>'员工头像','type'=>'image'),
			array('name'=>'phone','title'=>'电话','type'=>'string'),
			//array('name'=>'lev','title'=>'等级','type'=>'string'),
			array('name'=>'lev_name','title'=>'等级名称','type'=>'string'),
			//array('name'=>'service','title'=>'可服务项目','type'=>'string'),
			array('name'=>'isshow','title'=>'是否首页','type'=>'switch','set'=>'set'),
			array('name'=>'status','title'=>'状态','type'=>'switch','set'=>'set'),
			array('name'=>'islock','title'=>'是否锁定','type'=>'switch','set'=>'set'),
		)),
		'search'=>array(
			array('name'=>'name','title'=>'员工姓名','type'=>'string','width'=>'15'),
			array('name'=>'phone','title'=>'电话','type'=>'string','width'=>'15'),
			array('name'=>'status','title'=>'状态','type'=>'select','options'=>'status_select','width'=>'15'),
		)
	);
	$return['status_select']=array(array('id'=>'0','title'=>'关闭'),array('id'=>'1','title'=>'开启'));
	$return['htmls']=$htmls;
	set_html('framess',$this->menu());
	exi($return);
}
if($_RQ['op']=='edit'){
	$return=array();
	$id=intval($_RQ['id']);
	if($id){
		$item=pdo_get('jy_meiye_worker',array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
		
	}else{
		$item=array();
	}
	$return['item']=$item;
	$htmls=array(
		'type'=>'edit',
		'post'=>'post',
		'tabs'=>array(
			array('op'=>'list','title'=>'返回员工列表'),
		),
		'edit_tab'=>true,
		'edits'=>array(
			array(
				'title'=>'基本信息',
				'list'=>array(
					array('name'=>'name','title'=>'员工姓名','type'=>'string','ismust'=>true),
					array('name'=>'headimg','title'=>'员工头像','type'=>'image','ismust'=>true),
					array('name'=>'phone','title'=>'电话','type'=>'string','help'=>'填写电话将有短信通知'),
					array('name'=>'lev','title'=>'等级名称','type'=>'select','options'=>'lev','option_title'=>'lev_name','option_value'=>'id'),
					array('name'=>'years','title'=>'从事行业年限','type'=>'string'),
					array('type'=>'choose','title'=>'选择会员','name'=>'mid','action'=>'get_member','choose_title'=>'选择会员'),
					array('name'=>'age','title'=>'年纪','type'=>'string'),
					array('name'=>'service','title'=>'可服务项目','type'=>'checkbox','options'=>'service','ismust'=>true),
					array('name'=>'isshow','title'=>'是否首页','type'=>'switch'),
					array('name'=>'status','title'=>'状态','type'=>'switch'),
					array('name'=>'islock','title'=>'是否锁定','type'=>'switch'),
					array('name'=>'remark','title'=>'员工介绍','type'=>'text'),
				)
			),
		)
	);
	$service=pdo_getall('jy_meiye_serve',array('uniacid'=>$_SESSION['uniacid']),array('id','title'),'id');
	foreach($service as &$row){
		$row=$row['title'];
	}
	$return['service']=$service;
	$return['lev']=pdo_getall('jy_meiye_lev',array('uniacid'=>$_SESSION['uniacid']),array('id','lev_name'));
	$return['htmls']=$htmls;
	exi($return);
}
if($_RQ['op']=='post'){
	$postdata=get_postdata();
	if(empty($postdata['name'])){
		exi('请完善员工姓名','error');
	}
	if(empty($postdata['headimg'])){
		exi('请完善员工头像','error');
	}
	if(!$postdata['lev']){
		exi('请完善员工等级','error');
	}
	if(!$postdata['mid']){
		exi('请选择员工会员mid','error');
	}
	$data=array(
		'name'=>trim($postdata['name']),
		'phone'=>intval($postdata['phone']),
		'headimg'=>trim($postdata['headimg']),
		'lev'=>intval($postdata['lev']),
		'years'=>intval($postdata['years']),
		'age'=>intval($postdata['age']),
		'service'=>trim($postdata['service']),
		'remark'=>trim($postdata['remark']),
		'status'=>intval($postdata['status']),
		'mid'=>intval($postdata['mid']),
		'isshow'=>intval($postdata['isshow']),
		'islock'=>intval($postdata['islock']),
	);
	$id=intval($postdata['id']);
	if($id){
		pdo_update('jy_meiye_worker',$data,array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
	}else{
		$data['uniacid']=$_SESSION['uniacid'];
		pdo_insert('jy_meiye_worker',$data);
	}
	exi('操作成功！','','list');
}
if($_RQ['op']=='del'){
	$del=trim($_RQ['del'],';');
	if($del){
		$del=explode(';',$del);
	}else{
		$del=array();
	}
	if($_RQ['id']){
		$del[]=$_RQ['id'];
	}
	foreach($del as $id){
		$id=intval($id);
		if($id){
			pdo_delete('jy_meiye_worker',array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
		}
	}
	exi('删除成功！');
}
if($_RQ['op']=='set'){
	$id=intval($_RQ['id']);
	if(!$id){
		exi('请先选择要编辑的员工！','error');
	}
	$post=get_postdata();
	if(isset($post['status'])){
		pdo_update('jy_meiye_worker',array('status'=>intval($post['status'])),array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
	}
	if(isset($post['isshow'])){
		pdo_update('jy_meiye_worker',array('isshow'=>intval($post['isshow'])),array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
	}
	if(isset($post['islock'])){
		pdo_update('jy_meiye_worker',array('islock'=>intval($post['islock'])),array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
	}
	exi('操作成功！');
}